import { defineComponent, ref } from 'vue';
import { useRouter } from 'vue-router'
import request from '@/request'
import dayjs from 'dayjs'
import { BLOG_STATUS } from '@/dictionaries'

export default defineComponent({
  name: 'controlUser',
  setup() {
    const router = useRouter()
    const searchBlogName = ref('')   //搜索博客名称
    const blogName = ref('')
    const value = ref('')
    const scDisabled = ref(true)  //按钮能否使用 删除
    const lookBlog = ref(true)  //按钮能否使用 查看
    const alterBlog = ref(true)  //按钮能否使用 查看
    const selection = ref([])//复选框
    const blogData = ref([])//博客数据
    const deleteBlogId = ref([])//删除集合
    const OneColBlogId = ref('')//当前行博客

    // 通过 title搜索博客
    const blogSearch = ()=>{
      request('api/blogs/list','post',{bTitle:searchBlogName.value}).then(
        (res)=>{
          blogData.value = res.data
        }
      )
    }
    // 重置
    const resetBlogSearch =()=>{
      searchBlogName.value =''
    }

    // 复选框
    const handleSelectionChange = (val) => {
      if (val.length == 0) {
        scDisabled.value = true
        lookBlog.value = true
        alterBlog.value = true
      } else if (val.length == 1) {
        scDisabled.value = false
        lookBlog.value = false
        alterBlog.value = false
      } else {
        scDisabled.value = false
        lookBlog.value = true
        alterBlog.value = true
      }
      selection.value = val;
    }

    // 获取当前行数据

    // 删除选中数据
    const fnBlogDelete = () => {
      selection.value.map(x=>{
        deleteBlogId.value.push(x.bId)
      })
      request('/api/blogs/delete','DELETE',{bIds:deleteBlogId.value}).then(
      (res)=>{
        // console.log(res)
        fnBlogList()
      })
    }

    // 跳转创建博客
    const toCreateBlog = () => {
      router.push('/createBlog')
    }
    const OneColUid = ref('');
    // 跳转修改博客
    const toUpdateBlog = () => {
      selection.value.forEach((blogValue)=>{
        OneColUid.value = blogValue.bId
      })
      const bId = OneColUid.value
      router.push('/updateBlog/'+bId)
    }
    // 跳转博客展示
    const toUserBlog =()=>{
      selection.value.forEach((blogValue)=>{
        OneColUid.value = blogValue.bId
      })
      const bId = OneColUid.value
      router.push('/userBlog/'+bId)
    }

    // 博客列表
    const fnBlogList = () =>{
      request('/api/blogs/list','post',{}).then(
        (res)=>{
          blogData.value = res.data
          console.log(res.data)
        },
      )
    }
    //时间格式化
    const timeForMat = (row, column, cellValue,) =>{
      let time
      if (cellValue){
        time = dayjs(cellValue).format('YYYY-MM-DD')
      }else {
        time = '---'
      }
      return time
    }
    return {
      blogName,
      value,
      lookBlog,
      alterBlog,
      scDisabled,
      searchBlogName,
      fnBlogDelete,
      toUpdateBlog,
      toCreateBlog,
      blogData,
      handleSelectionChange,
      fnBlogList,
      timeForMat,
      toUserBlog,
      blogSearch,
      resetBlogSearch,
      BLOG_STATUS
    }
  },
  components: {},
  created() {
    this.fnBlogList()
  }
});
